package morenoapp.components.thinkgraphv2.nodes.partial;

import edu.umd.cs.piccolo.nodes.PPath;
import edu.umd.cs.piccolox.nodes.PComposite;
import java.awt.Color;
import java.awt.Stroke;
import java.util.Random;

/**
 *
 * @author root
 */
public class PVector extends PComposite {
    //coordinates
    float startX;
    float startY;
    float endX;
    float endY;
    //visual
    private PPath lineBody;
    private PPath lineLeftLeaf;
    private PPath lineRightLeaf;

    public PVector(
            double _startX,
            double _startY,
            double _endX,
            double _endY,
            float _startOffset,
            float _endOffset,
            Stroke _stroke,
            Color _color) {
        super();
        final float leafLength = 9.0f;
        startX = (float)_startX;
        startY = (float)_startY;
        endX = (float)_endX;
        endY = (float)_endY;
        double lengthX = _endX - _startX;
        double lengthY = _endY - _startY;
        float length = (float) Math.sqrt(lengthX * lengthX + lengthY * lengthY);
        if (length == 0) {
            return;
        }
        double angle = Math.atan2(lengthY, lengthX);

        float realStartOffset = _startOffset;
        if ((_startOffset + _endOffset) * 1.5 < length) {
            length -= _endOffset;
        } else {
            realStartOffset = 0.0f;
        }

        float realStartX = (float) (_startX + realStartOffset * Math.cos(angle));
        float realStartY = (float) (_startY + realStartOffset * Math.sin(angle));
        float realEndX = (float) (_startX + length * Math.cos(angle));
        float realEndY = (float) (_startY + length * Math.sin(angle));

        //body
        lineBody = new PPath();
        lineBody.moveTo(realStartX, realStartY);
        lineBody.lineTo(realEndX, realEndY);
        lineBody.setStroke(_stroke);
        lineBody.setStrokePaint(_color);
        lineBody.setPickable(false);

        //left leaf
        lineLeftLeaf = PPath.createLine(realEndX, realEndY,
                (float) (realEndX + leafLength * (float) Math.cos(angle + Math.PI + 0.15)),
                (float) (realEndY + leafLength * (float) Math.sin(angle + Math.PI + 0.15)));
        lineLeftLeaf.setPickable(false);

        //right leaf
        lineRightLeaf = PPath.createLine(realEndX, realEndY,
                (float) (realEndX + leafLength * (float) Math.cos(angle + Math.PI - 0.15)),
                (float) (realEndY + leafLength * (float) Math.sin(angle + Math.PI - 0.15)));
        lineRightLeaf.setPickable(false);
        

        addChild(lineBody);
        addChild(lineLeftLeaf);
        addChild(lineRightLeaf);

        setPickable(false);
        
        
        
    }

}
